<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of soaUsers
 *
 * @author dxx
 */

namespace app\common\model\activity;

class SalesActivity extends \think\Model {

    protected $table = 'sales_activity';
    protected $connection = ['database' => 'activity'];

    /**
     * 获取一条记录通过条件
     * @param type $condition 条件
     */
    public function getOne($condition=[]){
        return $this->where($condition)->find();
    }
    
    /**
     * 获取活动列表
     * @param type $params
     */
    public function getList($params) {
         
        if (empty($params['company_id'])) {
            return false;
        }
        $list = $where = [];
        if (!empty($params['keywords'])) {
            $where['activity_name'] = ['like',"%{$params['keywords']}%"];
        }
        if (!empty($params['status'])) {
            $where['status'] = $params['status'];
        }
        $where['company_id'] = $params['company_id'];
        
        $count = $this->where($where)->count();
        
        if (!empty($count)) {
            $page = isset($params['page']) ? (int) $params['page'] : 1;
            $pageSize = isset($params['pageSize']) ? (int) $params['pageSize'] : 10;
            
            $list = $this->where($where)->page($page,$pageSize);
            if(!empty($params['order'])){
                $list = $list->order($params['order']);
            }
            $list = $list->select();
        }

        return [
            'page'  => (int) $page,
            'count' => (int) $count,
            'list'  => $list
        ];
    }
    
    
     /**
     * 更新活动
     * @param type $activityId
     * @param type $data
     * @return type
     */
    public function updateData($activityId, $data) {
        return $this->where(['id'=>$activityId])->update($data);
    }
    
    /**
     * 保存叽叽券活动id
     * @param type $activityId
     * @param type $data
     * @return type
     */
    public function addData($data) {
        return $this->insertGetId($data);
    }
    
    
    /**
     * 获取已投放渠道列表
     * @param type $params
     * @return boolean
     */
    public function servedActivity($params) {

        $list = $where = [];
        if (!empty($params['keywords'])) {
            $where['channel_name'] = ['like',"%{$params['keywords']}%"];
        }
        if (isset($params['channel_status'])) {
            $where['sales_activity_channel.status'] = $params['channel_status'];
        }
        $where['sales_channel.company_id'] = $params['company_id'];
        $where['activity_id'] = $params['activity_id'];
        $table = 'sales_activity_channel';
        
        $count = $this->setTable($table)->alias('sales_activity_channel')->join('sales_channel sales_channel'
            ,'sales_activity_channel.channel_id = sales_channel.id','left')
                ->where($where)->count();
        
        if ($count) {
            $page = isset($params['page']) ? (int) $params['page'] : 1;
            $pageSize = isset($params['pageSize']) ? (int) $params['pageSize'] : 10;

            $list = $this->setTable($table)->alias('sales_activity_channel')
                    ->join('sales_channel sales_channel'
            ,'sales_activity_channel.channel_id = sales_channel.id','left')
            ->field('sales_activity_channel.id,
                activity_id,
                channel_id,
                sales_activity_channel.status,
                sales_activity_channel.add_date,
                sales_activity_channel.company_id,
                get_number, use_number,
                pv, uv, channel_name,
                sales_channel.status as channel_status')
                ->where($where)->page($page,$pageSize)->select();
        }
        return [
            'page'  => (int) $page,
            'count' => (int) $count,
            'list'  => $list
        ];
    }
}